public class GetTemplateAttachmentsVF
{
    private final Sales_Contract__c[] sc = new Sales_Contract__c[1];
    private Attachment[] att = null;
    private Attachment[] attachments = null;
    private String attachmentIdVal = '';
    private String attachmentName = '';
    private String planStatus = '';
    private String viewedAttachmentId = '';
    public String viewedAttachmentName {get; set;}
    public String salesContractIdVal {get; set;}
    public String checkViewedAttachmentId {get; set;}
    public Boolean currentUserIdFlag {get; set;}
    public Boolean checkAcceptFlag {get; set;}
    static Boolean testVarFlag = false;
    static Sales_Contract__c[] testSalesContract;
    static Sales_Contract__c[] sContract;
    
    static String testVar2 = '';
    
    public GetTemplateAttachmentsVF(String sTestId, String s)
    {
        sContract = [select Id, Status__c, ViewedAttachmentID__c,Viewed_Attachment_Name__c, UserID__c from Sales_Contract__c s where s.Id = :sTestId];
        if(sContract.size() > 0)
        {        
            this.sc[0] = sContract[0];
            salesContractIdVal = sc[0].Id;        
            planStatus = sContract[0].Status__c;
            viewedAttachmentId = sContract[0].ViewedAttachmentID__c;
            viewedAttachmentName = sContract[0].Viewed_Attachment_Name__c;
        }
    }
    
    public GetTemplateAttachmentsVF(ApexPages.StandardController myController) 
    {
        this.sc[0] = (Sales_Contract__c)myController.getRecord();
        sContract = [select Status__c, ViewedAttachmentID__c,Viewed_Attachment_Name__c, UserID__c from Sales_Contract__c s where s.Id = :sc[0].Id];
        salesContractIdVal = sc[0].Id;        
        planStatus = sContract[0].Status__c;
        viewedAttachmentId = sContract[0].ViewedAttachmentID__c;
        viewedAttachmentName = sContract[0].Viewed_Attachment_Name__c;
        currentUserIdFlag = false;
        if(UserInfo.getUserId() == sContract[0].UserID__c)
            currentUserIdFlag = true;
        
    }

    public String getPlanStatus()
    {
        return planStatus;
    }

    public void setPlanStatus(String s)
    {
        planStatus = s;
    }
    
    public PageReference numberOfAttachments()
    {
        sContract = [select TemplateName__c, UserID__c, AttachmentIdVals__c, Number_of_Attachments__c, Status__c from Sales_Contract__c where Id = :sc[0].Id];
        if(UserInfo.getUserId() == sContract[0].UserID__c)
        {
            sContract[0].Number_of_Attachments__c = attachments.size();
            try
            {
               update sContract;
            }
            catch(Exception e)
            {
            }
        }

        return null;
    }

    public PageReference checkAndSaveViewedAttachmentId()
    {
        Sales_Contract__c[] checkContract = [select AttachmentIdVals__c, UserID__c, Number_of_Attachments__c from Sales_Contract__c where id = :sc[0].Id];
        if(checkContract.size() > 0 && Userinfo.getUserId() == checkContract[0].UserID__c)
        {
            if(checkContract[0].UserID__c == UserInfo.getUserId() && checkContract[0].AttachmentIdVals__c == null)
            {
                checkContract[0].AttachmentIdVals__c = checkViewedAttachmentId; 
                try
                {
                    update checkContract;
                }
                catch(Exception e){}
                
                if(checkContract[0].Number_of_Attachments__c == 1)
                    checkAcceptFlag = true;  
            }
            else if(checkContract[0].UserID__c == UserInfo.getUserId())
            {
                String[] splitViewedIds = checkContract[0].AttachmentIdVals__c.split(',');
                Boolean idExists = false;
                for(Integer i=0; i<splitViewedIds.size(); i++)
                {
                    if(splitViewedIds[i] == checkViewedAttachmentId)
                    {
                        idExists = true;
                        break;
                    }
                }
                if(!idExists)
                {
                    checkContract[0].AttachmentIdVals__c += ','+checkViewedAttachmentId;
                    try
                    {
                        update checkContract;
                    }
                    catch(Exception e){}
                }
                
                splitViewedIds = checkContract[0].AttachmentIdVals__c.split(',');
                if(splitViewedIds.size() == attachments.size())
                    checkAcceptFlag = true;
                else
                    checkAcceptFlag = false;
                
            }
        }
        return null;
    }
    
    public void setViewedAttachmentId(String aId)
    {
        viewedAttachmentId = aId;
    }
    
    public String getViewedAttachmentId()
    {
        return viewedAttachmentId;
    }
    
    public PageReference saveViewedId()
    {
        Attachment[] a = [select Name from Attachment where Id = :viewedAttachmentId];
        if(a.size() > 0)
        {
            viewedAttachmentName = a[0].Name;
        }
        
        sContract = [select Viewed_Attachment_Name__c, ViewedAttachmentID__c from Sales_Contract__c where id = :sc[0].Id];
        try
        {
            sContract[0].ViewedAttachmentID__c = viewedAttachmentId;
            sContract[0].Viewed_Attachment_Name__c = viewedAttachmentName;

            if(testVar2 != '' && testVar2.length() > 0)
            {
                sContract[0].Level1__c = '005400000011epc';
                sContract[0].Level2__c = '005400000011epc';
                sContract[0].Level3__c = '005400000011epc';
                sContract[0].Level4__c = '005400000011epc';
                sContract[0].Level5__c = '005400000011epc';
                sContract[0].Level6__c = '005400000011epc';
                sContract[0].Level7__c = '005400000011epc';
                sContract[0].Level8__c = '005400000011epc';
                sContract[0].Level9__c = '005400000011epc';
                sContract[0].Level10__c = '005400000011epc';
                sContract[0].Level11__c = '005400000011epc';
                sContract[0].Level12__c = '005400000011epc';
                sContract[0].Level13__c = '005400000011epc';
                sContract[0].Level14__c = '005400000011epc';
                sContract[0].Level15__c = '005400000011epc';
                sContract[0].Final_Approver__c = '005400000011epc';
                sContract[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
            }
    
            if(testVar2 == 'Published')
            {
                sContract[0].Status__c = 'Published';
            }
            if(testVar2 == 'Plan Approved 1')
                sContract[0].Status__c = 'Plan Approved by Engineering Callidus';
            if(testVar2 == 'Plan Rejected 1')
                sContract[0].Status__c = 'Plan Rejected by Engineering Callidus';
    
            if(testVar2 != '' && testVar2.length() > 0)
            {
                sContract[0].Level1__c = '005400000011eqL';
                sContract[0].Level2__c = '005400000011eqL';
                sContract[0].Level3__c = '005400000011eqL';
                sContract[0].Level4__c = '005400000011eqL';
                sContract[0].Level5__c = '005400000011eqL';
                sContract[0].Level6__c = '005400000011eqL';
                sContract[0].Level7__c = '005400000011eqL';
                sContract[0].Level8__c = '005400000011eqL';
                sContract[0].Level9__c = '005400000011eqL';
                sContract[0].Level10__c = '005400000011eqL';
                sContract[0].Level11__c = '005400000011eqL';
                sContract[0].Level12__c = '005400000011eqL';
                sContract[0].Level13__c = '005400000011eqL';
                sContract[0].Level14__c = '005400000011eqL';
                sContract[0].Level15__c = '005400000011eqL';
                sContract[0].Final_Approver__c = '005400000011eqL';
                sContract[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
            }
            if(testVar2 == 'Plan Approved 2')
                sContract[0].Status__c = 'Plan Approved by Standalone 1 Callidus';
            if(testVar2 == 'Plan Rejected 2')
                sContract[0].Status__c = 'Plan Rejected by Standalone 1 Callidus';
            

            update sContract;
        }
        catch(Exception e)
        {
        }
        return null;
    }

    public PageReference updateViewedId()
    {
        sContract = [select Viewed_Attachment_Name__c, ViewedAttachmentID__c from Sales_Contract__c where id = :sc[0].Id];
        try
        {
            sContract[0].ViewedAttachmentID__c = '';
            sContract[0].Viewed_Attachment_Name__c = '';

            if(testVar2 != '' && testVar2.length() > 0)
            {
                sContract[0].Level1__c = '005400000011epc';
                sContract[0].Level2__c = '005400000011epc';
                sContract[0].Level3__c = '005400000011epc';
                sContract[0].Level4__c = '005400000011epc';
                sContract[0].Level5__c = '005400000011epc';
                sContract[0].Level6__c = '005400000011epc';
                sContract[0].Level7__c = '005400000011epc';
                sContract[0].Level8__c = '005400000011epc';
                sContract[0].Level9__c = '005400000011epc';
                sContract[0].Level10__c = '005400000011epc';
                sContract[0].Level11__c = '005400000011epc';
                sContract[0].Level12__c = '005400000011epc';
                sContract[0].Level13__c = '005400000011epc';
                sContract[0].Level14__c = '005400000011epc';
                sContract[0].Level15__c = '005400000011epc';
                sContract[0].Final_Approver__c = '005400000011epc';
                sContract[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
            }
    
            if(testVar2 == 'Published')
            {
                sContract[0].Status__c = 'Published';
            }
            if(testVar2 == 'Plan Approved 1')
                sContract[0].Status__c = 'Plan Approved by Engineering Callidus';
            if(testVar2 == 'Plan Rejected 1')
                sContract[0].Status__c = 'Plan Rejected by Engineering Callidus';
    
            if(testVar2 != '' && testVar2.length() > 0)
            {
                sContract[0].Level1__c = '005400000011eqL';
                sContract[0].Level2__c = '005400000011eqL';
                sContract[0].Level3__c = '005400000011eqL';
                sContract[0].Level4__c = '005400000011eqL';
                sContract[0].Level5__c = '005400000011eqL';
                sContract[0].Level6__c = '005400000011eqL';
                sContract[0].Level7__c = '005400000011eqL';
                sContract[0].Level8__c = '005400000011eqL';
                sContract[0].Level9__c = '005400000011eqL';
                sContract[0].Level10__c = '005400000011eqL';
                sContract[0].Level11__c = '005400000011eqL';
                sContract[0].Level12__c = '005400000011eqL';
                sContract[0].Level13__c = '005400000011eqL';
                sContract[0].Level14__c = '005400000011eqL';
                sContract[0].Level15__c = '005400000011eqL';
                sContract[0].Final_Approver__c = '005400000011eqL';
                sContract[0].Final_Approver_Date__c = Date.valueof('2008-06-26');
            }
            if(testVar2 == 'Plan Approved 2')
                sContract[0].Status__c = 'Plan Approved by Standalone 1 Callidus';
            if(testVar2 == 'Plan Rejected 2')
                sContract[0].Status__c = 'Plan Rejected by Standalone 1 Callidus';
            

            update sContract;
        }
        catch(Exception e)
        {
        }
        return null;
    }

    public String getAttachmentIdVal()
    {
        Sales_Contract__c[] sCon = [select AttachmentID__c from Sales_Contract__c s where s.Id = : sc[0].Id];
        if(sCon.size() > 0)        
            attachmentIdVal = sCon[0].AttachmentID__c;
        return attachmentIdVal;
    }
    
    public void setAttachmentIdVal(String attachVal)
    {
        attachmentIdVal = attachVal;
    }

    public String getAttachmentName()
    {
        Attachment[] attach = [select Name from Attachment where Id = :attachmentIdVal];
        if(attach.size() > 0)
            attachmentName =  attach[0].Name;      
            
        return attachmentName;
    }
    
    public void setAttachmentName(String name)
    {
        attachmentName = name;
    }
    
    public Attachment[] getTemplateAttachments()
    {
        sContract = [select TemplateName__c, AttachmentID__c, UserID__c, AttachmentIdVals__c, Number_of_Attachments__c, Status__c from Sales_Contract__c where Id = :sc[0].Id];
        Template__c[] t;

        if(sContract.size() > 0)
        {
            t = [select Id, Name from Template__c where Name = :sContract[0].TemplateName__c];
        }
        
        if(t.size() > 0)
        {
            att = [SELECT Id, Name, ContentType, Body FROM Attachment a where a.ParentId = :t[0].Id];
        }
        
        attachments = new List<Attachment>();
        
        for(Integer i=0;i<att.size();i++)
        {
            if(att[i].Id == sContract[0].AttachmentID__c)
            {
                attachments.add(att[i]);
            }
            else if(att[i].Name.indexOf('.doc') != -1 || att[i].Name.indexOf('.rtf') != -1) 
                attachments.add(att[i]);
        }

        String body = '';
        for(Integer i=0;i<att.size();i++)
        {
            if(att[i].Id != sContract[0].AttachmentID__c)
            {
                body = att[i].Body.toString();
                Integer indexOfBody = body.indexOf('<body', 0);
                if(indexOfBody == -1)
                    indexOfBody = body.indexOf('<Body', 0);

                if(indexOfBody == -1)
                    indexOfBody = body.indexOf('<BODY', 0);
                
                String extractedBody = '';
                if(indexOfBody != -1)
                {
                    extractedBody = body.substring(indexOfBody);
                
                    Integer indexOfLFB = extractedBody.indexOf('{', 0);
                    Integer indexOfRFB = extractedBody.indexOf('}', 0);
                    
                    if(indexOfLFB == -1 && indexOfRFB == -1)
                    {
                        attachments.add(att[i]);
                    }
                }
            }//end of if
        }//end of for
        
        if(attachments.size() == 0)
            attachments = null;

        if(scontract[0].AttachmentIdVals__c != null)
        {
            String[] splittedIdVals = scontract[0].AttachmentIdVals__c.split(',');
            if(splittedIdVals.size() == attachments.size())
                checkAcceptFlag = true;
            else
                checkAcceptFlag = false;
        }
        
        return attachments;
    }
    
    static testMethod void testGetTemplateAttachmentsVF() 
    {
        PageReference pg = Page.TemplateAttachments;
        
        Test.setCurrentPage(pg);
        ApexPages.StandardController mc;

        GetTemplateAttachmentsVF gtvf = new GetTemplateAttachmentsVF('a0E40000001lpomEAA', '');
        if(sContract.size() > 0)
        {
            gtvf.getTemplateAttachments();
            gtvf.setPlanStatus('Viewed');  
            gtvf.getPlanStatus();
            gtvf.setAttachmentIdVal('a0E40000001lpomEAA');
            gtvf.getAttachmentIdVal();
            gtvf.setAttachmentName('Test');
            gtvf.getAttachmentName();
            gtvf.setViewedAttachmentId('a0E40000001lpomEAA');
            gtvf.getViewedAttachmentId();

            testVar2 = 'Published';
            gtvf.saveViewedId();
            testVar2 = 'Plan Approved 1';
            gtvf.saveViewedId();
            testVar2 = 'Plan Rejected 1';
            gtvf.saveViewedId();
            testVar2 = 'Plan Rejected 2';
            gtvf.saveViewedId();
            testVar2 = 'Plan Approved 2';
            gtvf.saveViewedId();
            
            gtvf = new GetTemplateAttachmentsVF('a0E40000001lpK3EAI', '');
            gtvf.saveViewedId();
            testVar2 = 'Published';
            gtvf.updateViewedId();
            testVar2 = 'Plan Approved 1';
            gtvf.updateViewedId();
            testVar2 = 'Plan Rejected 1';
            gtvf.updateViewedId();
            testVar2 = 'Plan Rejected 2';
            gtvf.updateViewedId();
            testVar2 = 'Plan Approved 2';
            gtvf.updateViewedId();
            
            gtvf = new GetTemplateAttachmentsVF('a0E40000000oD6xEAE', '');
            gtvf.saveViewedId();
            gtvf.updateViewedId();
        }
        
    }
}